import java.util.HashMap;
import java.util.Map;

public class Test {
    class Solution {
        public int totalFruit(int[] fruits) {
            Map<Integer,Integer> hash = new HashMap<>();
            int ret = 0;
            int left = 0;
            int right = 0;
            int n = fruits.length;

            while(right < n) {
                int in = fruits[right];
                hash.put(in,hash.getOrDefault(in,0)+1);
                while(hash.size() > 2) {
                    int out = fruits[left];
                    hash.put(out,hash.get(out)-1);
                    if(hash.get(out) == 0) {
                        hash.remove(out);
                    }
                    left ++;
                }
                ret  = Math.max(ret,right-left+1);
                right ++;
            }
            return ret;
        }
    }
}
